# -*- coding: utf-8 -*-
import os
import shutil

from PyQt4.QtCore import QObject, SIGNAL, SLOT, QString, Qt
from PyQt4.QtGui import *

from config.preferences import SYSTEM, DADOS_DB

try:
    _fromUtf8 = QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s


class ActionsUiFinalizarPedido():

    def __init__(self, uiFin, db, num_pedido):
        """
            Construtor.
        """
        self.uiFin = uiFin
        self.db = db
        self.num_pedido = num_pedido
        self.connectSlot()
        self.preencherTabela()



    def connectSlot(self):
        """
        
        """
        QObject.connect(
                        self.uiFin.pushButtonImprimir,
                        SIGNAL("clicked()"),
                        self.pushButtonImprimirClicked)
        QObject.connect(
                        self.uiFin.pushButtonVoltar,
                        SIGNAL("clicked()"),
                        self.pushButtonVoltarClicked)
        QObject.connect(
                        self.uiFin.pushButtonFechar,
                        SIGNAL("clicked()"),
                        self.pushButtonFecharClicked)



    def pushButtonImprimirClicked(self):
        """
        
        """
        if SYSTEM == "linux2":
            response = self.db.getPlotPath(self.num_pedido)
            for plot in response:
                shutil.copy(
                plot[0],
                DADOS_DB + "print/")
            
            os.chdir(DADOS_DB + "print/")
            QFileDialog.getOpenFileName()
        
        else:
            response = self.db.getPlotPath(self.num_pedido)
            for plot in response:
                shutil.copy(
                            plot[0],
                            "//192.168.1.250/vna/dados/print")
            
            os.chdir("//192.168.1.250/vna/dados/print")
            QFileDialog.getOpenFileName()



    def pushButtonVoltarClicked(self):
        """
        
        """
        pass



    def pushButtonFecharClicked(self):
        """
        
        """
        self.uiFin.dialog.close()
        
        if SYSTEM == "linux2":
            os.chdir(DADOS_DB + "print/")
            arquivos = os.listdir(DADOS_DB + "print/")
            if len(arquivos) == 0:
                pass
            else:
                for arq in arquivos:
                    os.remove(arq)
        else:
            os.chdir("//192.168.1.250/vna/dados/print")
            arquivos = os.listdir("//192.168.1.250/vna/dados/print")
            if len(arquivos) == 0:
                pass
            else:
                for arq in arquivos:
                    os.remove(arq)



    def preencherTabela(self):
        """
        
        """
        i = 0
        response = self.db.pesquisaFinalizarPedido(self.num_pedido)
        print response
        self.uiFin.tableWidget.setSortingEnabled(False)
        self.uiFin.tableWidget.setRowCount(len(set(response)))
        headers = ["   Produto    ", "Quantidade"]
        self.uiFin.tableWidget.setColumnCount(len(headers))
        self.uiFin.tableWidget.setHorizontalHeaderLabels(headers)
        
        for prod in set(response):
            qt = self.db.pesquisaPorNumPedidoIdProduto(prod[0], self.num_pedido)
            produto = self.db.getNomeProduto(prod[0])
            self.uiFin.tableWidget.setItem(
                                            i,
                                            0,
                                            QTableWidgetItem(
                                            str(produto[0])))
            self.uiFin.tableWidget.setItem(
                                            i,
                                            1,
                                            QTableWidgetItem(
                                            str(qt)))
            i += 1
        self.uiFin.tableWidget.setSortingEnabled(True)
        self.uiFin.tableWidget.resizeColumnsToContents()
        self.uiFin.tableWidget.setFocus()
